<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cdkeytype
 *
 * @author Dragon.hu add @2015.12.7
 */
defined('BASEPATH') or die('forbidden');

class Cdkeytype_Controller extends Controller {

	private $inc;
	private $channels;
	private $tmpconnect;	//数据库连接
	
	function _init() {
		check_level(50, segment(0), segment(1), segment(2, 'index'));
		//set_time_limit(0);
		/*$this->channels = array(
				'0' => '渠道无关', 'uc' => 'UC', 'baidu' => 'Baidu', 'sanliuling' => '360',
				'xiaomi' => 'Xiaomi', 'lianxiang' => 'Lianxiang', 'oppo' => 'Oppo', 'huawei' => 'Huawei'
		);*/
		$this->inc = $this->config['inc'];
		$this->channels = $this->inc['allchannel'];
		array_push($this->channels, '渠道无关');
		
		$this->tmpconnect = $this->load->database('db') or die("load failed!".mysql_error());
	}

	function index() {
		$this->view->display("cdkey/cdkeytype/index");
	}

	function flexigrid() {
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

		$sql = "SELECT COUNT(*) AS num FROM cdkey_type WHERE {$where}"; 
		
		$one = $this->tmpconnect->one($sql);
		$total = $one['num'];
		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
		
		$sql = "SELECT {$colsinfo} FROM cdkey_type where {$where}  {$orderby} LIMIT {$offset},{$rp}";
		
		$this->tmpconnect->query($sql);
		
		while ($n = $this->tmpconnect->get_array('default'))
		{
			$n['channel'] = $this->channels[$n['channel']];
			$rows[] = array('id' => $n['autoid'], 'cell' => $n);
		}
		
		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $rows
		);
		
		echo json_encode($result);
	}
	
	function add()
	{
		$sql = "SELECT * FROM cdkey_type";
		$this->tmpconnect->query($sql);
		while($n = $this->tmpconnect->get_array('default'))
		{
			$tmparr[] = $n;
		}
		
		if (is_submit())
		{
			$rowdata = $_POST['row'];
			//1.激活码类型不能重复
			for ($i = 0; $i < count($tmparr); $i++)
			{
				if ($tmparr[$i]['type'] == $rowdata['keytype'])
				{
					echo "<script>alert('type can not repeat !!!');</script>";
					$this->view->assign ("channel", $this->form->select ("row[channel]", $this->channels, '', array('id' => 'channel')));
					$this->view->display("cdkey/cdkeytype/add");
					return;
				}
			}
			//2.加入数据库中
			$sql = "INSERT INTO cdkey_type (channel, type, name, giftbagid) VALUES ('0', ".$rowdata['keytype'].", '".$rowdata['name']."', '".$rowdata['giftbagid']."')";
			if($this->tmpconnect->query($sql))
			{
				cp_tips("success");
			}
			else
			{
				cp_tips("error");
			}
		}
		$this->view->assign ("channel", $this->form->select ("row[channel]", $this->channels, '', array('id' => 'channel')));
		$this->view->display("cdkey/cdkeytype/add");
	}
	
	function edit($id = 0)
	{	
		if ($id === 0)
			error(translate('C_INVALID_PARAM_ID', $id));
		$row = $this->tmpconnect->one("SELECT * FROM cdkey_type WHERE autoid=$id");
		if (!is_array($row))
			error(translate('C_INVALID_PARAM_ID', $id));
		
		if (is_submit())
		{
			$rowdata = $_POST['row'];
			
			$sql = "UPDATE cdkey_type SET name ='".$rowdata['name']."', giftbagid='".$rowdata['giftbagid']."' WHERE autoid=$id";
			if($this->tmpconnect->query($sql))
			{
				cp_tips("success");
			}
			else
			{
				cp_tips("error");
			}
		}

		//$this->view->assign('channel', $this->form->select('row[channel]', $this->channels, array('channel' => $row['channel']), array('id' => 'channel')));
		$this->view->display("cdkey/cdkeytype/edit", array('row' => $row));
	}

	function del($id = 0)
	{ 
		$c = 0;
		$r = '';
		if ($id === 0)
			json_output($c, translate('C_INVALID_PARAM_ID', $id));
		$row = $this->tmpconnect->one("SELECT * FROM cdkey_type WHERE autoid=$id");
		if (!is_array($row))
			json_output($c, translate('C_INVALID_PARAM_ID', $id));
		
		if ($this->tmpconnect->delete('cdkey_type', array('autoid' => $id))) {
			insert_log(translate('CDKEY_KEYTYPE'),translate('DEL'), array('autoid'=> $id));
			$c = 1;
		} else {
			$r = translate('C_TIPS_ERROR_TEXT');
		}
		json_output($c, $r);
	}
}
